//
//  NSBundle+QTAdd.m
//  Pods
//
//  Created by 张俊博 on 16/8/22.
//
//

#import "NSBundle+QTAdd.h"
#import "NSString+QTAdd.h"
#import "QTCategoryMacro.h"

QTSYNTH_DUMMY_CLASS(NSBundle_QTAdd)

@implementation NSBundle (QTAdd)

+ (NSArray *)preferredScales {
    static NSArray *scales;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        CGFloat screenScale = [UIScreen mainScreen].scale;
        if (screenScale <= 1) {
            scales = @[@1,@2,@3];
        } else if (screenScale <= 2) {
            scales = @[@2,@3,@1];
        } else {
            scales = @[@3,@2,@1];
        }
    });
    return scales;
}

+ (NSString *)pathForScaledResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)bundlePath {
    if (name.length == 0) return nil;
    if ([name hasSuffix:@"/"]) return [self pathForResource:name ofType:ext inDirectory:bundlePath];
    
    NSString *path = nil;
    NSArray *scales = [self preferredScales];
    for (int s = 0; s < scales.count; s++) {
        CGFloat scale = ((NSNumber *)scales[s]).floatValue;
        NSString *scaledName = ext.length ? [name stringByAppendingNameScale:scale]
        : [name stringByAppendingPathScale:scale];
        path = [self pathForResource:scaledName ofType:ext inDirectory:bundlePath];
        if (path) break;
    }
    
    return path;
}

- (NSString *)pathForScaledResource:(NSString *)name ofType:(NSString *)ext {
    if (name.length == 0) return nil;
    if ([name hasSuffix:@"/"]) return [self pathForResource:name ofType:ext];
    
    NSString *path = nil;
    NSArray *scales = [NSBundle preferredScales];
    for (int s = 0; s < scales.count; s++) {
        CGFloat scale = ((NSNumber *)scales[s]).floatValue;
        NSString *scaledName = ext.length ? [name stringByAppendingNameScale:scale]
        : [name stringByAppendingPathScale:scale];
        path = [self pathForResource:scaledName ofType:ext];
        if (path) break;
    }
    
    return path;
}

- (NSString *)pathForScaledResource:(NSString *)name ofType:(NSString *)ext inDirectory:(NSString *)subpath {
    if (name.length == 0) return nil;
    if ([name hasSuffix:@"/"]) return [self pathForResource:name ofType:ext];
    
    NSString *path = nil;
    NSArray *scales = [NSBundle preferredScales];
    for (int s = 0; s < scales.count; s++) {
        CGFloat scale = ((NSNumber *)scales[s]).floatValue;
        NSString *scaledName = ext.length ? [name stringByAppendingNameScale:scale]
        : [name stringByAppendingPathScale:scale];
        path = [self pathForResource:scaledName ofType:ext inDirectory:subpath];
        if (path) break;
    }
    
    return path;
}

+ (NSBundle *)QTResourceBundle
{
    return [NSBundle QTBundleWithName:@"QTResourceBundle"];
}

+ (NSBundle *)QTUILoginBundle
{
    return [NSBundle QTBundleWithName:@"QTUILoginBundle"];
}

+ (NSBundle *)QTUIOfficialBundle
{
    return [NSBundle QTBundleWithName:@"QTUIOfficialBundle"];
}

+ (NSBundle *)QTUIFileBundle
{
    return [NSBundle QTBundleWithName:@"QTUIFileBundle"];
}

+ (NSBundle *)QTUIMutiTeamBundle
{
    return [NSBundle QTBundleWithName:@"QTUIMutiTeamBundle"];
}

+ (NSBundle *)QTUIMineBundle
{
    return [NSBundle QTBundleWithName:@"QTUIMineBundle"];
}

+ (NSBundle *)QTUIContactBundle
{
    return [NSBundle QTBundleWithName:@"QTUIContactBundle"];
}

+ (NSBundle *)QTUIChatBundle
{
    return [NSBundle QTBundleWithName:@"QTUIChatBundle"];
}

+ (NSBundle *)QTBundleWithName:(NSString *)bundleName
{
    return [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:bundleName withExtension:@"bundle"]];
}

- (NSBundle *)zh_HansBundle
{
    NSBundle *bundle = [NSBundle bundleWithPath:[self pathForResource:@"zh-Hans" ofType:@"lproj"]];
    if (bundle) {
        return bundle;
    } else {
        return self;
    }
}

- (NSBundle *)enBundle
{
    NSBundle *bundle = [NSBundle bundleWithPath:[self pathForResource:@"en" ofType:@"lproj"]];
    if (bundle) {
        return bundle;
    } else {
        return self;
    }
}

//+ (NSBundle *)QTBundleZh_Hans
//{
//    return [NSBundle bundleWithPath:[[NSBundle QTResourceBundle] pathForResource:@"zh-Hans" ofType:@"lproj"]];
//}
//
//+ (NSBundle *)QTBundleEn
//{
//    return [NSBundle bundleWithPath:[[NSBundle QTResourceBundle] pathForResource:@"en" ofType:@"lproj"]];
//}

@end
